package com.titleevaluation.controller;

import com.titleevaluation.entity.ContinuingEducation;
import com.titleevaluation.service.ContinuingEducationService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/applications/{applicationId}/continuing-education")
public class ContinuingEducationController {

    private final ContinuingEducationService continuingEducationService;

    public ContinuingEducationController(ContinuingEducationService continuingEducationService) {
        this.continuingEducationService = continuingEducationService;
    }

    @PostMapping
    public ResponseEntity<ContinuingEducation> createContinuingEducation(@PathVariable Long applicationId, @RequestBody ContinuingEducation continuingEducation) {
        ContinuingEducation createdContinuingEducation = continuingEducationService.createContinuingEducation(applicationId, continuingEducation);
        return ResponseEntity.ok(createdContinuingEducation);
    }

    @GetMapping
    public ResponseEntity<List<ContinuingEducation>> getContinuingEducationByApplicationId(@PathVariable Long applicationId) {
        List<ContinuingEducation> continuingEducation = continuingEducationService.getContinuingEducationByApplicationId(applicationId);
        return ResponseEntity.ok(continuingEducation);
    }

    @GetMapping("/{id}")
    public ResponseEntity<ContinuingEducation> getContinuingEducationById(@PathVariable Long id) {
        return continuingEducationService.getContinuingEducationById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PutMapping("/{id}")
    public ResponseEntity<ContinuingEducation> updateContinuingEducation(@PathVariable Long id, @RequestBody ContinuingEducation continuingEducation) {
        ContinuingEducation updatedContinuingEducation = continuingEducationService.updateContinuingEducation(id, continuingEducation);
        return ResponseEntity.ok(updatedContinuingEducation);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteContinuingEducation(@PathVariable Long id) {
        continuingEducationService.deleteContinuingEducation(id);
        return ResponseEntity.noContent().build();
    }
}

